package piis;

import Algorithms.LinearRegression;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import utilities.PiisWriter;
import utilities.Utilities;

/**
 *
 * @author Atuan
 */
public class LinearRegressionDM {
    
    private static String path = "src/test4";
    private static int dimensions;
    private static int degree;
    private static double[] min;
    private static double[] max;
    private static List<List<Double>> x = new ArrayList<>();
    private static List<Double> y = new ArrayList<>();
    private static double scalFactor = 10;
    
    
    
    public static void readMultiLRData(String filename){
        BufferedReader br = null;        
        try {
            String line;
            br = new BufferedReader(new FileReader(path+"/"+filename));
            degree = Integer.parseInt(br.readLine());
            dimensions = Integer.parseInt(br.readLine());
            String nn = br.readLine();
            String[] minS = nn.split(" ");
            String[] maxS = br.readLine().split(" ");
            min = new double[minS.length];
            max = new double[maxS.length];
            for(int i = 0; i < minS.length; i++){
                min[i] = Double.parseDouble(minS[i]);
                max[i] = Double.parseDouble(maxS[i]);
            }
            x = new ArrayList<>();
            y = new ArrayList<>();
            while ((line = br.readLine()) != null) {
                String[] lineArray = line.split(" ");
                String[] lineArr = line.split("  ");
                List<Double> tmp = new ArrayList<>();
                for(int j = 0; j < dimensions; j++){
                    tmp.add(Double.parseDouble(lineArray[j])/scalFactor);
                }
                x.add(tmp);
                y.add(Double.parseDouble(lineArr[lineArr.length-1])/scalFactor);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null){
                    br.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        File dir = new File(path);
        String[] files = dir.list();
        
        if (files == null){
            System.out.println("Empty directory");
        } else { 
            for (int i=0;i<files.length;i++){
                if(files[i].endsWith(".in") && files[i].startsWith("linreg")){
                    readMultiLRData(files[i]);
                    Double[] yD = new Double[y.size()];
                    yD = y.toArray(yD);
                    double[] yy = new double[y.size()];
                    for(int j = 0; j < y.size(); j++){
                        yy[j] = yD[j];
                    }
                    LinearRegression lr = new LinearRegression(x,yy,degree,dimensions);
                    lr.gradientDescentSI();
                    
                    String newname = files[i].replace(".in", "");
                    
                    PiisWriter.write(lr.getTrainInput(), path+"/"+newname + "_train.out");
                    List<String> outInput = new ArrayList<>();
                    
                    double[][] trainSet = Utilities.generateTrainingSet(min, max, 0.5);
                    
                    for(int k = 0; k < trainSet.length; k++){
                        String out = "";
                        double[] temp = new double[trainSet[k].length];
                        for(int l = 0; l < trainSet[k].length; l++){
                            temp[l] = trainSet[k][l]/scalFactor;
                            out += Double.toString(trainSet[k][l]) + " ";
                        }
                        out += Double.toString(lr.func(temp,degree)*scalFactor);
                        outInput.add(out);
                    }
                    PiisWriter.write(outInput, path+"/"+newname + "_result.out");
                }
            }
        }        
    }
}
